package com.gbei.center.oauth.controller;

import com.gbei.center.base.BaseController;
import com.gbei.center.config.restauth.RestAuthConfig;
import com.gbei.center.oauth.service.ApiService;
import com.gbei.center.req.open.ReqAreaCode;
import com.gbei.center.req.open.ReqThirdpartyPlaceOrder;
import com.gbei.center.req.open.ReqUserCommon;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;

/**
 * 通过Access Token访问的API服务
 *
 */
@RestController
@RequestMapping("api")
@Api(description = "提供外部接口")
public class ApiController extends BaseController{
    private static Logger log = LoggerFactory.getLogger(ApiController.class);

    @Autowired
    private ApiService apiService;



    /**
     * 根据token获取用户信息
     * @param
     * @return
     */
    @RestAuthConfig(release = true)
    @ApiOperation(value = "根据token获取用户信息")
    @PostMapping(value = "user/getInfo", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getUserInfo(@Valid @RequestBody ReqUserCommon req, BindingResult br){
        log.info("根据token获取用户信息接口：{}", req);
        checkParams(br);
        return apiService.getUserInfo(req);
    }


    @RestAuthConfig(release = true)
    @ApiOperation(value = "开放平台注册用户", response = String.class)
    @PostMapping("open/user/userCommon")
    public String userCommon(@Valid @RequestBody ReqUserCommon req, BindingResult br) {
        log.info("开放平台注册用户：{}", req);
        checkParams(br);
        String apiUrl = "open/user/userCommon";
       return apiService.userCommon(req,apiUrl);
    }



    @ApiOperation(value = "下单/修改/查询/退款")
    @RestAuthConfig(release = true)
    @PostMapping("open/order/orderCommon")
    public String placeOrder(@Valid @RequestBody ReqThirdpartyPlaceOrder req, BindingResult br) throws Exception {
        log.info("第三方下单/修改/查询/退款接口：{}", req);
        checkParams(br);
        String apiUrl = "open/order/orderCommon";
        return apiService.placeOrder(req,apiUrl);

    }



    @ApiOperation(value = "获取行政区域编码")
    @RestAuthConfig(release = true)
    @PostMapping("open/area/areaCommon")
    public String getAreaCode(@Valid @RequestBody ReqAreaCode req, BindingResult br) throws Exception {
        log.info("第三方获取行政区域编码接口：{}", req);
        checkParams(br);
        String apiUrl = "open/area/areaCommon";
        return apiService.getAreaCode(req,apiUrl);
    }



}
